/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.CFields;
import DTO.Detail_project;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Detail_projectBUS extends IData<Detail_project>{

    @Override
    public Detail_project add(Detail_project item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Detail_project item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Detail_project> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Detail_project Find(Detail_project item, List<Detail_project> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(Detail_project item, int t, SubConfig config) {
        Object result = 0;
        String mysql = "";
        PreparedStatement cs;      
        try{
            if(t==0){
            mysql = "INSERT INTO "+CFields.detail_project+"(" +
                CFields.project_id + "," + 
                CFields.user_id + "," + 
                CFields.description + "," + 
                CFields.state + "," + 
                CFields.completed + "," +
                CFields.priority + "," +
                CFields.begin_date + "," + 
                CFields.end_date + "," + 
                CFields.finish_date+ "," + 
                CFields.hours +"," + 
                CFields.expire_date +"," + 
                CFields.isdelete +") " +
                " VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);  
                cs.setLong(1, item.getProject_id());
                cs.setLong(2, item.getUser_id());
                cs.setString(3, item.getDescription());
                cs.setLong(4, item.getState());
                cs.setDouble(5, item.getCompleted());
                cs.setLong(6, item.getPriority());
                cs.setTimestamp(7, item.getBegin_date());
                cs.setTimestamp(8, item.getEnd_date());
                cs.setTimestamp(9, item.getFinish_date());
                cs.setDouble(10, item.getHours());
                cs.setLong(11, item.getExpire_date());
                cs.setBoolean(12, item.isIsdelete());
                
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }else if (t==1){
                mysql = "update " + CFields.detail_project +" set " +
                CFields.project_id + " = ?, " + 
                CFields.user_id + " = ?,  " + 
                CFields.description + " = ?, " + 
                CFields.state + " = ?, " + 
                CFields.completed + " = ?, " +
                CFields.priority + " = ?, " +
                CFields.begin_date + " = ?, " + 
                CFields.end_date + " = ?, " + 
                CFields.finish_date+ " = ?, " + 
                CFields.hours + " = ?, " +
                CFields.expire_date + " = ?, " + 
                CFields.isdelete + " = ? " +
                " Where (" + CFields.id + "= ?);";
                cs = config.connec.prepareStatement(mysql);  
                
                cs.setLong(1, item.getProject_id());
                cs.setLong(2, item.getUser_id());
                cs.setString(3, item.getDescription());
                cs.setLong(4, item.getState());
                cs.setDouble(5, item.getCompleted());
                cs.setLong(6, item.getPriority());
                cs.setTimestamp(7, item.getBegin_date());
                cs.setTimestamp(8, item.getEnd_date());
                cs.setTimestamp(9, item.getFinish_date());
                cs.setDouble(10, item.getHours());
                cs.setLong(11, item.getExpire_date());
                cs.setBoolean(12, item.isIsdelete());
                cs.setLong(13, item.getId());
                
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1); 
                }
            }else if (t==2){
                mysql = "update " + CFields.detail_project +" set " +
                CFields.isdelete+ "= true " +
                "Where (" + CFields.id + "= ?);";
                cs = config.connec.prepareStatement(mysql);  
                
                cs.setLong(1, item.getId());
                
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);  
            }
            }
        } catch (SQLException ex) {
                Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }       
        return result;
    }

    @Override
    public Object insertOnSubmit(Detail_project item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Detail_project item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }

    @Override
    public Object deleteOnSubmint(Detail_project item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    @Override
    protected Detail_project getItem(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Detail_project> getAll(SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
